package Test01;

// 定义基类 Vehicle
class Vehicle {
    String make;
    String model;

    public Vehicle(String make, String model) {
        this.make = make;
        this.model = model;
    }

    // 虚方法 drive
    public void drive() {
        System.out.println("Driving a vehicle.");
    }
}

// 定义子类 Car，继承自 Vehicle
class Car extends Vehicle {
    public Car(String make, String model) {
        super(make, model);
    }

    // 重写 drive 方法
    @Override
    public void drive() {
        System.out.println("Driving a car.");
    }
}

// 定义子类 Bike，继承自 Vehicle
class Bike extends Vehicle {
    public Bike(String make, String model) {
        super(make, model);
    }

    // 重写 drive 方法
    @Override
    public void drive() {
        System.out.println("Riding a bike.");
    }
}

// 主程序
 public class VehicleManagement {
    public static void main(String[] args) {
        // 声明一个 Vehicle 类型的数组，用于存储 Car 和 Bike 对象
        Vehicle[] vehicles = new Vehicle[3];

        // 初始化数组，创建几个 Car 和 Bike 对象，并将它们存储在数组中
        vehicles[0] = new Car("Toyota", "Corolla");
        vehicles[1] = new Bike("Yamaha", "YZF-R1");
        vehicles[2] = new Car("Honda", "Civic");

        // 使用循环遍历数组，并通过多态性调用每个对象的 drive 方法
        for (Vehicle vehicle : vehicles) {
            vehicle.drive();
        }
    }
}
